# FindBullet.cmake
#
# Copyright 2010 Johan Waldeback
# This file is released under GPL v3 lisence
#
# set environent variables:
# Bullet_ROOT
# Bullet_INCLUDE_DIRS (optional)
# Bullet_LIBRARY_DIRS / Bullet_LIBS (recomended)
#



#
# macro BULLET_ERROR_MESSAGE - prints an error message
#
# If required, then error messages becomes fatal errors
# If not required, then error messages becomes status messages
#
macro( BULLET_ERROR_MESSAGE _msg )
  if( BULLET_FIND_REQUIRED )
    message( FATAL_ERROR ${_msg} )
  else()
    message( STATUS ${_msg} )
  endif()
endmacro()

#
# macro BULLET_STATUS_MESSAGE - prints an status message
#
# If not quiet - prints a status message
# If quiet - prints nothing
#
macro( BULLET_STATUS_MESSAGE _msg )
  if(NOT BULLET_FIND_QUIET )
    message( STATUS ${_msg} )
  endif()
endmacro()

#
# Find the Bullet_ROOT
#

# If BULLET_ROOT was defined in the environment, use it.
FIND_PATH( Bullet_ROOT NAMES src/btBulletCollisionCommon.h
  PATHS
    $ENV{BULLET_ROOT}
    "C:/bullet-2.76"
    "C:/bullet-2.73"
)


#print status message
#if( NOT "${Bullet_ROOT}" STREQUAL "Bullet_ROOT-NOTFOUND") 
    BULLET_STATUS_MESSAGE( "Found bullet root: ${Bullet_ROOT}" )
#endif()


SET(Bullet_FOUND TRUE)

#
# Find the include dir
#

# If BULLET_INCLUDE_DIRS was defined in the environment, use it.
find_path(Bullet_INCLUDE_DIRS NAMES btBulletCollisionCommon.h
    PATHS
      $ENV{BULLET_INCLUDE_DIRS}
      ${Bullet_ROOT}/include
      ${Bullet_ROOT}/src
  )

if( "${Bullet_INCLUDE_DIRS}" STREQUAL "Bullet_INCLUDE_DIRS-NOTFOUND" )
  BULLET_ERROR_MESSAGE(  "Bullet - Include dir: * NOT FOUND *" )
  SET( Bullet_FOUND FALSE)
else()
  BULLET_STATUS_MESSAGE(  "Bullet - Include dir: ${Bullet_INCLUDE_DIRS}" )
endif()

#
# FIND_LIBRARY macro
#

#macro( FIND_LIBRARY_PATH _var, _configuration )

  #DEBUG
#  message( " FIND_LIBRARY_PATH : _${_var}_ : ${_configuration}" )

  # set _LIB_SEARCH_DIRS
#  SET( _LIB_SEARCH_DIRS ${Bullet_ROOT} )
#  foreach( _DIR ${ADDITIONAL_LIBRARY_SEARCH_DIRS} ) 
#     SET( _LIB_SEARCH_DIRS ${_LIB_SEARCH_DIRS}
#       ${_DIR}/${_configuration}
#       ${_DIR}
#    )
#  endforeach()
 
  #DEBUG
#  message( "_LIB_SEARCH_DIRS = ${_LIB_SEARCH_DIRS}" ) 

  # Find  indevidual componentes dirs
#  SET(${_var}_FOUND TRUE)
#  foreach( COMPONENT ${Bullet_FIND_COMPONENTS} )
#    message ("...${COMPONENT}..." )

#    find_library( Bullet_${COMPONENT}_DIR_${_configuration} NAMES ${COMPONENT}
#      PATHS
#        ${_LIB_SEARCH_DIRS}
#      PATH_SUFFIXES lib 
#    )
#    IF( Bullet_${COMPONENT}_DIR_${_configuration} ) 
#      SET(Bullet_${COMPONENT}_DIR_${_configuration}_FOUND TRUE)
#message( "${_configuration} / ${COMPONENT} found: ${Bullet_${COMPONENT}_DIR_${_configuration}}" )
#    else()
#      SET(${_var}_FOUND FALSE)
#message( "${_configuration} / ${COMPONENT} not found" )
#    endif()
#  endforeach()

  #Generatre the path
#  IF(${_var}_FOUND) 
#    SET(_var "") 
#    foreach( COMPONENT ${Bullet_FIND_COMPONENTS} )
#      SET( _var ${_var} ${Bullet_${COMPONENT}_DIR_${_configuration}} )
#    endforeach()
#  endif()

# message( "var =  ${_var}" )

#endmacro()



#FIND_LIBRARY_PATH( "Bullet_LIBRARY_DIRS_RELEASE", "Release" )
#FIND_LIBRARY_PATH( "Bullet_LIBRARY_DIRS_DEBUG", "Debug" )
#FIND_LIBRARY_PATH( "Bullet_LIBRARY_DIRS_MINSIZEREL", "MinSizeRel" )
##FIND_LIBRARY_PATH( "Bullet_LIBRARY_DIRS_RELWITHDEBINFO", "RelWithDebInfo" )

#
# Find the defult library dir
#
FOREACH( COMPONENT ${Bullet_FIND_COMPONENTS})
  SET( _libname "${COMPONENT}.lib" )
  SET( _path "_path-NOTFOUND" )
  FIND_PATH( _path NAMES "${_libname}" 
    PATHS
      $ENV{BULLET_LIBRARY_DIRS}
      $ENV{BULLET_LIBS}
      ${Bullet_ROOT}/lib
  )
  
  IF( "${_path}" STREQUAL "_path-NOTFOUND" )
    SET(Bulllet_FOUND FALSE)
    BULLET_ERROR_MESSAGE("Bullet unable to find: ${_libname}")
  ELSE()
    LIST(FIND "${_path}" Bullet_LIBRARY_DIRS _found)
    IF( _found LESS 0 )
      SET( Bullet_LIBRARY_DIRS ${Bullet_LIBRARY_DIRS} "${_path}" )
    ENDIF()
    
    LIST(FIND "${_libname}" Bullet_LIBRARIES _found)
    IF( _found LESS 0 )
      SET( Bullet_LIBRARIES "${Bullet_LIBRARIES}" "${_libname}" )
    ENDIF()
    
  ENDIF()
ENDFOREACH()


BULLET_STATUS_MESSAGE("Bullet library dir: ${Bullet_LIBRARY_DIRS}")
BULLET_STATUS_MESSAGE("Bullet libraries: ${Bullet_LIBRARIES}")
BULLET_STATUS_MESSAGE("Bullet found: ${Bullet_FOUND} ")



